package com.example.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.entity.PaymentInfo;
import com.example.mapper.PaymentInfoMapper;
import com.example.service.PaymentInfoService;
import com.example.vo.AliPay;
import com.example.vo.AliPaySync;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.time.LocalDateTime;

/**
* @author circle48
* @description 针对表【payment_info(支付信息表)】的数据库操作Service实现
* @createDate 2024-06-06 20:13:04
*/
@Service
public class PaymentInfoServiceImpl extends ServiceImpl<PaymentInfoMapper, PaymentInfo>
    implements PaymentInfoService{


    @Override
    public void insert(AliPay alipay) {
        PaymentInfo paymentInfo = new PaymentInfo();
        paymentInfo.setOrderIds(alipay.getBody());
        paymentInfo.setAlipayTradeNo(alipay.getOut_trade_no());
        paymentInfo.setTotalAmount(new BigDecimal(alipay.getTotal_amount()));
        paymentInfo.setSubject(alipay.getSubject());
        paymentInfo.setPaymentStatus("PENDING");
        paymentInfo.setCreateTime(LocalDateTime.now());

        // 5、插入支付信息到数据库
        baseMapper.insert(paymentInfo);
    }

    @Override
    public void updatePaymentInfo(AliPaySync aliPaySync) {
        // 通过支付宝交易流水号找到支付信息，并更新状态和确认时间。
        PaymentInfo paymentInfo = baseMapper.findByAlipayTradeNo(aliPaySync.getOut_trade_no());
        if (paymentInfo != null) {
            paymentInfo.setPaymentStatus("PAID");
            paymentInfo.setConfirmTime(LocalDateTime.now());
            paymentInfo.setCallbackContent(aliPaySync.toString());
            paymentInfo.setCallbackTime(LocalDateTime.now());
            paymentInfo.setAlipayTradeNo(aliPaySync.getTrade_no());
            baseMapper.updateById(paymentInfo);
        }
    }


    @Override
    public String getOrderIds(String out_trade_no) {
        return baseMapper.getOrderIds(out_trade_no);
    }
}




